package com.ecy.quadratic.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.ComicClassifyOneThirdAdapter;
import com.ecy.quadratic.beans.ComicClassify;
import com.ecy.quadratic.ui.activity.ComicListActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/31.
 */
public class ComicCategoryFragment extends Fragment {

    private GridView mGridView;

    //    private ListView mComicListView;
    private View mRootView;
    private ComicClassifyOneThirdAdapter mClassifyAdapter;


    private List<ComicClassify> mComicList;

    public ComicCategoryFragment() {
        mComicList = new ArrayList<ComicClassify>();
        ComicClassify comicClassify = new ComicClassify();
        comicClassify.setId("young");
        comicClassify.setTitle("少年");
        comicClassify.setImgId(R.drawable.classify_young);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("girl");
        comicClassify.setTitle("少女");
        comicClassify.setImgId(R.drawable.classify_girl);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("life");
        comicClassify.setTitle("生活");
        comicClassify.setImgId(R.drawable.classify_life);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("thriller");
        comicClassify.setTitle("惊悚");
        comicClassify.setImgId(R.drawable.classify_thriller);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("tanbi");
        comicClassify.setTitle("耽美");
        comicClassify.setImgId(R.drawable.classify_tanbi);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("city");
        comicClassify.setTitle("都市");
        comicClassify.setImgId(R.drawable.classify_noveled);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("love");
        comicClassify.setTitle("恋爱");
        comicClassify.setImgId(R.drawable.classify_love);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("fantasy");
        comicClassify.setTitle("魔幻");
        comicClassify.setImgId(R.drawable.classify_fantasy);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("joke");
        comicClassify.setTitle("搞笑");
        comicClassify.setImgId(R.drawable.classify_joke);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("action");
        comicClassify.setTitle("动作");
        comicClassify.setImgId(R.drawable.classify_action);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("ancient");
        comicClassify.setTitle("古风");
        comicClassify.setImgId(R.drawable.classify_ancient);
        mComicList.add(comicClassify);
        comicClassify = new ComicClassify();
        comicClassify.setId("across");
        comicClassify.setTitle("穿越");
        comicClassify.setImgId(R.drawable.classify_through);
        mComicList.add(comicClassify);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return initView(inflater, container);
    }

    private View initView(LayoutInflater inflater, ViewGroup container) {
        long time = System.currentTimeMillis() / 1000;
        System.out.println("request:initView enter:" + time);
        if (mRootView != null) {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null) {
                parent.removeView(mRootView);
            }
            return mRootView;
        }
        mRootView = inflater.inflate(R.layout.fragment_comic_classify, null);

        mGridView = (GridView) mRootView.findViewById(R.id.recommend_classify_gridview);

        mClassifyAdapter = new ComicClassifyOneThirdAdapter(getContext(), mComicList);
        mGridView.setAdapter(mClassifyAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ComicListActivity.startActivity(getContext(), mComicList.get(position));
            }
        });
        return mRootView;
    }

    /**
     * 利用线程池定时执行动画轮播
     */
    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}
